/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JProdcutHistoryView.java
 *
 * Created on 2010-6-5, 9:55:23
 */

package cn.ekuma.epos.analysis.product;

import cn.ekuma.epos.order.model.OrderLineVTableModel;
import cn.ekuma.util.SwingUtil;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.gui.MessageInf;
import java.awt.Color;
import java.awt.event.ItemEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.text.DecimalFormat;
import javax.swing.DefaultComboBoxModel;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.labels.StandardXYSeriesLabelGenerator;
import org.jfree.chart.plot.CombinedDomainXYPlot;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.StandardXYItemRenderer;
import org.jfree.chart.renderer.xy.XYAreaRenderer;
import org.jfree.chart.renderer.xy.XYBarRenderer;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;

/**
 *
 * @author Administrator
 */
public class JProdcutHistoryView extends javax.swing.JPanel{

    JFreeChart priceValueChart;
    AnalysisModelManager manager;
    OrderLineVTableModel orderLineTableModel;
    /** Creates new form JProdcutHistoryView */
    public JProdcutHistoryView(AnalysisModelManager manager) {
        initComponents();
        this.manager=manager;
        priceValueChart=createChart();
        jPriceValuePanel.add(new ChartPanel(priceValueChart),java.awt.BorderLayout.CENTER);
        manager.addPropertyChangeListener( new PropertyChangeListener(){
            public void propertyChange(PropertyChangeEvent evt) {
                if(evt.getPropertyName().equals(AnalysisModelManager.PROPERTY_DialyProductID)){
                    String[] value=(String[])evt.getNewValue();
                    reflush(value[0]);
                    priceValueChart.setTitle(value[1]);
                }
          }
        });
        orderLineTableModel=new OrderLineVTableModel();
        jXTable1.setAutoCreateRowSorter(true);
        jXTable1.setColumnControlVisible(true);
        jXTable1.setModel(orderLineTableModel);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jSplitPane1 = new javax.swing.JSplitPane();
        jPanel1 = new javax.swing.JPanel();
        jPriceValuePanel = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jComboBox1 = new javax.swing.JComboBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        jXTable1 = new org.jdesktop.swingx.JXTable();

        setLayout(new java.awt.BorderLayout());

        jSplitPane1.setDividerLocation(400);
        jSplitPane1.setDividerSize(8);
        jSplitPane1.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);
        jSplitPane1.setOneTouchExpandable(true);

        jPanel1.setLayout(new java.awt.BorderLayout());

        jPriceValuePanel.setLayout(new java.awt.BorderLayout());
        jPanel1.add(jPriceValuePanel, java.awt.BorderLayout.CENTER);

        jPanel2.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT));

        jComboBox1.setPreferredSize(new java.awt.Dimension(100, 21));
        jComboBox1.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jComboBox1ItemStateChanged(evt);
            }
        });
        jPanel2.add(jComboBox1);

        jPanel1.add(jPanel2, java.awt.BorderLayout.PAGE_END);

        jSplitPane1.setLeftComponent(jPanel1);

        jXTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jXTable1);

        jSplitPane1.setRightComponent(jScrollPane1);

        add(jSplitPane1, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents

    private void jComboBox1ItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jComboBox1ItemStateChanged
        String selectedEntry=(String) jComboBox1.getSelectedItem();
        if(evt.getStateChange()==ItemEvent.SELECTED&&selectedEntry!=null){
            manager.createVolumeDataset(selectedEntry);
            orderLineTableModel.resert(manager.getOrderLineByOrderType(selectedEntry));
            SwingUtil.PreferredJXTableColumn(jXTable1);
        }
    }//GEN-LAST:event_jComboBox1ItemStateChanged


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPriceValuePanel;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSplitPane jSplitPane1;
    private org.jdesktop.swingx.JXTable jXTable1;
    // End of variables declaration//GEN-END:variables

    public void reflush(String id){
        try {
            manager.initChartModel(id);
            jComboBox1.setModel(new DefaultComboBoxModel(manager.getTransactionColumns().toArray()));
            if (manager.getTransactionColumns().size() > 0) {
                orderLineTableModel.resert(manager.getOrderLineByOrderType(manager.getTransactionColumns().get(0)));
                SwingUtil.PreferredJXTableColumn(jXTable1);
            }
        } catch (BasicException ex) {
            new MessageInf(ex).show(this);
        }
    }

    private  JFreeChart createChart() {
        CombinedDomainXYPlot plot = new CombinedDomainXYPlot(
                new DateAxis(""));
        
        NumberAxis rangeAxis1 = new NumberAxis("");
        rangeAxis1.setLowerMargin(0.40);  // to leave room for volume bars
        DecimalFormat format = new DecimalFormat("00.00");
        rangeAxis1.setNumberFormatOverride(format);
        XYPlot subplot = new XYPlot(
                manager.getPricetimeSeries(), null, rangeAxis1,
                new StandardXYItemRenderer());
        subplot.setBackgroundPaint(Color.lightGray);
        subplot.setDomainGridlinePaint(Color.white);
        subplot.setRangeGridlinePaint(Color.white);

        subplot.setDomainCrosshairVisible(true);
        subplot.setRangeCrosshairVisible(true);

        XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
        renderer.setBaseShapesVisible(true);
        renderer.setSeriesFillPaint(0, Color.red);
        renderer.setSeriesFillPaint(1, Color.white);

        renderer.setUseFillPaint(true);
        renderer.setLegendItemToolTipGenerator(new StandardXYSeriesLabelGenerator("Tooltip {0}"));
        subplot.setRenderer(0, renderer);

        NumberAxis rangeAxis2 = new NumberAxis("");
        rangeAxis2.setUpperMargin(1.00);  // to leave room for price line
        subplot.setRangeAxis(1, rangeAxis2);
        subplot.setDataset(1, manager.getValueTimeSeries());
        subplot.mapDatasetToRangeAxis(1, 1);
        XYBarRenderer renderer2 = new XYBarRenderer(0.2);
        renderer2.setDrawBarOutline(false);
        renderer2.setShadowVisible(false);

        //renderer2.setToolTipGenerator(
                //new StandardXYToolTipGenerator(
                //StandardXYToolTipGenerator.DEFAULT_TOOL_TIP_FORMAT,
                //new SimpleDateFormat("d-MMM-yyyy"), new DecimalFormat("0,000.00")));
        subplot.setRenderer(1, renderer2);
        plot.add(subplot);

        NumberAxis rangeAxis21 = new NumberAxis("");
        rangeAxis21.setUpperMargin(0.4);  // to leave room for price line
        rangeAxis21.setLowerMargin(0.4);  // to leave room for price line
        rangeAxis21.setAutoRange(true);
        
        XYPlot subplot1 = new XYPlot(
                manager.getStoreTimeSeries(), null, rangeAxis21,
                /*new XYLineAndShapeRenderer()renderer2*/new XYAreaRenderer() );
        subplot1.setBackgroundPaint(Color.lightGray);
        subplot1.setDomainGridlinePaint(Color.white);
        subplot1.setRangeGridlinePaint(Color.white);
        subplot1.setRangeCrosshairVisible(true);
        plot.add(subplot1);
        JFreeChart chart = new JFreeChart("", plot);
        return chart;
    }
}
